package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.CollectionRecordEntity;
import com.tfjybj.itoo.exam.model.*;
import com.tfjybj.itoo.exam.provider.service.CollectionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * CollectionRecordController
 * collectionRecord表
 *
 * @author 王雪芬
 * @version 2.0.0
 * @since 2.0.0 2019-01-26 18:24:57
 */
@Api(tags = {"collectionRecord表接口"})
@RequestMapping(value = "/collectionRecord")
@RestController
@Slf4j
public class CollectionRecordController {

    @Resource
    private CollectionRecordService collectionRecordService;

    /**
     * 插入错题
     *
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "添加错题")
    @PostMapping(value = {"/insertWrongRecord"})
    public ItooResult insertWrongRecord() {
        Boolean flag = collectionRecordService.insertWrongRecord();
        if (flag == true) {
            return ItooResult.build(ItooResult.SUCCESS, "考试错题存入成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询考试错题为空");
        }
    }

    /**
     * 根据考生ID查询错题课程和小练
     *
     * @param studentId
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "根据考生ID查询错题or收藏题课程和小练")
    @GetMapping(value = {"/queryCourseExamination/{status}/{studentId}"})
    public ItooResult queryCourseExamination(@PathVariable Integer status,
                                             @PathVariable String studentId) {
        if (StringUtils.isEmpty(status)) {
            return ItooResult.build(ItooResult.FAIL, "status为空");
        }
        if (StringUtils.isEmpty(studentId)) {
            return ItooResult.build(ItooResult.FAIL, "studentId为空");
        }
        Map<String, List<ExamCourseModel>> examCourseModelMap = collectionRecordService.queryCourseExamination(status, studentId);
        if (examCourseModelMap == null) {
            return ItooResult.build(ItooResult.FAIL, "没有相关课程信息");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", examCourseModelMap);
        }
    }


    /**
     * 学生交卷时把该学生的错题存入错题记录表
     *
     * @param examineeId
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "考生交卷时把考生的错题存入错题记录表")
    @GetMapping(value = {"/insertWrongByExamineeId/{examineeId}"})
    public ItooResult insertWrongByExamineeId(@PathVariable String examineeId) {
        if (examineeId == null) {
            return ItooResult.build(ItooResult.FAIL, "考生错题插入失败");
        }
        Boolean flag = collectionRecordService.insertWrongByExamineeId(examineeId);
        if (flag == true) {
            return ItooResult.build(ItooResult.SUCCESS, "考生错题插入成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "考生错题插入失败");
        }
    }

    /**
     * 根据小练和考生查询错题的章节
     *
     * @param status
     * @param examinationId
     * @param examineeId
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "根据错题小练显示章节")
    @GetMapping(value = {"/queryChapterType/{status}/{examinationId}/{examineeId}"})
    public ItooResult queryChapterType(@PathVariable Integer status, @PathVariable String examinationId, @PathVariable String examineeId) {
        if (StringUtils.isEmpty(status)) {
            return ItooResult.build(ItooResult.FAIL, "status为空");
        }
        if (StringUtils.isEmpty(examinationId)) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(examineeId)) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        List<ChapterQuestionTypeModel> chapterQuestionTypeModels = collectionRecordService.queryChapterType(status, examinationId, examineeId);
        if (chapterQuestionTypeModels == null) {
            return ItooResult.build(ItooResult.FAIL, "查询为空");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", chapterQuestionTypeModels);
        }
    }


    /**
     * 根据小练和考生章节查询错题题型
     *
     * @param status
     * @param examinationId
     * @param examineeId
     * @param chapterId
     * @return
     */
    @ApiOperation(value = "根据错题小练显示该章节的题型")
    @GetMapping(value = {"/queryQuestionType/{status}/{examinationId}/{examineeId}/{chapterId}"})
    public ItooResult queryQuestionType(@PathVariable Integer status, @PathVariable String examinationId, @PathVariable String examineeId, @PathVariable String chapterId) {
        if (StringUtils.isEmpty(status)) {
            return ItooResult.build(ItooResult.FAIL, "status为空");
        }
        if (StringUtils.isEmpty(examinationId)) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(examineeId)) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        if (StringUtils.isEmpty(chapterId)) {
            return ItooResult.build(ItooResult.FAIL, " chapterId为空");
        }

        List<ChapterQuestionTypeModel> chapterQuestionTypeList = collectionRecordService.queryQuestionType(status, examinationId, examineeId, chapterId);
        if (chapterQuestionTypeList == null) {
            return ItooResult.build(ItooResult.FAIL, "查询为空");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", chapterQuestionTypeList);
        }
    }

    /**
     * 根据小练，章节题型显示学生该题型错题信息-李娜-2019年1月29日
     *
     * @param examineeId
     * @param examinationId
     * @param questionTypeId
     * @param questionClassfiyId
     * @param status
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "根据小练，章节题型显示学生该题型错题信息")
    @GetMapping(value = {"/queryWrongQuestion/{examineeId}/{examinationId}/{questionTypeId}/{questionClassfiyId}/{status}/{templateId}"})
    public ItooResult queryWrongQuestion(@PathVariable String examineeId, @PathVariable String examinationId,
                                         @PathVariable String questionTypeId, @PathVariable String questionClassfiyId, @PathVariable Integer status, @PathVariable String templateId) {

        if (StringUtils.isEmpty(status)) {
            return ItooResult.build(ItooResult.FAIL, "status为空");
        }
        if (StringUtils.isEmpty(examinationId)) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(examineeId)) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        if (StringUtils.isEmpty(questionTypeId)) {
            return ItooResult.build(ItooResult.FAIL, " chapterId为空");
        }
        if (StringUtils.isEmpty(questionClassfiyId)) {
            return ItooResult.build(ItooResult.FAIL, " questionClassfiyId为空");
        }
        if (StringUtils.isEmpty(templateId)) {
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }

        ExamPaperModel examPaperModel = collectionRecordService.queryWrongQuestion(examineeId, examinationId, questionTypeId, questionClassfiyId, status, templateId);
        if (examPaperModel == null) {
            return ItooResult.build(ItooResult.FAIL, "查询错题失败");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "查询错题成功", examPaperModel);
        }
    }

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model CollectionRecordModel
     * @return 添加的结果
     * @author 王雪芬
     * @since 2.0.0 2019-01-26 18:24:57
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody CollectionRecordModel model) {
        if (StringUtils.isEmpty(model.getPaperRecordId())) {
            return ItooResult.build(ItooResult.FAIL, "paperRecordId为空");
        }
        if (StringUtils.isEmpty(model.getExamineeId())) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getExaminationId())) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionTypeId())) {
            return ItooResult.build(ItooResult.FAIL, "questionTypeId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionId())) {
            return ItooResult.build(ItooResult.FAIL, "questionId为空");
        }
        if (StringUtils.isEmpty(model.getVersion())) {
            return ItooResult.build(ItooResult.FAIL, "version为空");
        }
        CollectionRecordEntity collectionRecordEntity = new CollectionRecordEntity();
        BeanUtils.copyProperties(model, collectionRecordEntity);
        collectionRecordService.save(collectionRecordEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 王雪芬
     * @since 2.0.0 2019-01-26 18:24:57
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        collectionRecordService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 王雪芬
     * @since 2.0.0 2019-01-26 18:24:57
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        collectionRecordService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model CollectionRecordModel
     * @return 修改后的结果
     * @author 王雪芬
     * @since 2.0.0 2019-01-26 18:24:57
     */
    @ApiOperation(value = "根据id修改collectionRecord")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody CollectionRecordModel model) {
        if (StringUtils.isEmpty(model.getPaperRecordId())) {
            return ItooResult.build(ItooResult.FAIL, "paperRecordId为空");
        }
        if (StringUtils.isEmpty(model.getExamineeId())) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getExaminationId())) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionTypeId())) {
            return ItooResult.build(ItooResult.FAIL, "questionTypeId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionId())) {
            return ItooResult.build(ItooResult.FAIL, "questionId为空");
        }
        if (StringUtils.isEmpty(model.getVersion())) {
            return ItooResult.build(ItooResult.FAIL, "version为空");
        }
        CollectionRecordEntity collectionRecordEntity = new CollectionRecordEntity();
        BeanUtils.copyProperties(model, collectionRecordEntity);
        collectionRecordService.updateById(collectionRecordEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找CollectionRecord
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 王雪芬
     * @since 2.0.0 2019-01-26 18:24:57
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        CollectionRecordEntity collectionRecordEntity = collectionRecordService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", collectionRecordEntity);
    }

    /**
     * 分页查询所有CollectionRecord
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 王雪芬
     * @since 2.0.0 2019-01-26 18:24:57
     */
    @ApiOperation(value = "分页查询所有CollectionRecord")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<CollectionRecordEntity> collectionRecords = collectionRecordService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", collectionRecords);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 添加小题到CollectionRecord表
     *
     * @param model CollectionRecordModel
     * @return 添加小题到收藏夹
     * @author 刘梦娇
     * @since 2.0.0 2019年1月29日11:16:22
     */
    @ApiOperation(value = "添加小题到收藏夹CollectionRecordModel")
    @PostMapping(value = {"/insertCollection"})
    public ItooResult insertCollection(@RequestBody CollectionRecordModel model) {
//        @RequestBody CollectionRecordModel model
        boolean is_insert = collectionRecordService.insertCollection(model);
        if (!is_insert) {
            return ItooResult.build(ItooResult.FAIL, "已经添加过该小题", false);
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "添加成功", true);
        }

    }


//    boolean insertCollection(CollectionRecordModel model);

    /**
     * 删除CollectionRecord表
     *
     * @param
     * @return 取消收藏
     * @author 杨海云
     * @since 2.0.0 22019年2月10日
     */
    @ApiOperation(value = "取消收藏")
    @DeleteMapping(value = {"/cancelCollection/{examinationId}/{examineeId}/{questionId}"})
    public ItooResult cancelCollection(@PathVariable String examinationId,
                                       @PathVariable String examineeId,
                                       @PathVariable String questionId) {

        boolean iscollection = collectionRecordService.cancelCollection(examinationId, examineeId, questionId);
        if (iscollection == false) {
            return ItooResult.build(ItooResult.FAIL, "取消失败");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "取消成功", iscollection);
        }
    }


    /**
     * 课程练习-根据课程id和题型id查询试题及选项
     *
     * @param courseId
     * @param questionTypeId
     * @return 是否查询成功
     * @author 贾建清
     * @since 2.0.0 2019-3-24 19:14:31
     */
    @ApiOperation(value = "课程练习-根据课程id和题型id查询试题及选项")
    @GetMapping(value = {"/queryQuestionOption/{courseId}/{questionTypeId}"})
    public ItooResult queryQuestionOption(@PathVariable String courseId, @PathVariable String questionTypeId) {
        if (StringUtils.isEmpty(courseId)) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(questionTypeId)) {
            return ItooResult.build(ItooResult.FAIL, " questionTypeId为空");
        }
        List<QuestionMainModel> questionMainModelList = collectionRecordService.queryQuestionOption(courseId, questionTypeId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionMainModelList);
    }


}    
